home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / amipop17.zip / POP_DOPO.ORI < prev    next >
Text File  |  1993-11-15  |  6KB  |  420 lines

  1. #include "pop.h"
  2.  
  3. /* Variables global to this file */
  4.  
  5. struct Library *SockBase ;
  6. int havemail;
  7.  
  8. /* Functions */
  9.  
  10. int dopop(void)
  11. {
  12.     int s;
  13.     int count;
  14.     int okay=1;
  15.     struct hostent *hp;
  16.     struct sockaddr_in sa;
  17.  
  18.     sprintf(title,"Connecting to %s",pophost);
  19.     settitle();
  20.  
  21.     if((SockBase = OpenLibrary( "inet:libs/socket.library", 1L )) == NULL)
  22.     {
  23.         doreq("Error opening socket.library\n",bum);
  24.         return(1);
  25.     }
  26.     setup_sockets( MAXSOCKS, &errno );
  27.  
  28.     if((hp=gethostbyname(pophost))==NULL)
  29.     {
  30.         cleanup_sockets();
  31.         CloseLibrary( SockBase ) ;
  32.         doreq("Connection Refused.",bum);
  33.         return(1);
  34.     }
  35.  
  36.     bzero(&sa, sizeof(sa));
  37.     bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  38.     sa.sin_family = hp->h_addrtype;
  39.     sa.sin_port = htons((u_short)port);
  40.     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)
  41.     {
  42.         cleanup_sockets();
  43.         CloseLibrary( SockBase ) ;
  44.         doreq("Something bad\nhas happened.",bum);
  45.         return(1);
  46.     }
  47.  
  48.     if (connect(s,(struct sockaddr *) &sa,sizeof(sa))< 0)
  49.     {
  50.         doreq("No POP3 daemon\nrunning on this\nmachine or port.",bum);
  51.         s_close(s);
  52.         cleanup_sockets();
  53.         CloseLibrary( SockBase ) ;
  54.         return(1);
  55.     }
  56.  
  57. /* Put actual code here */
  58.  
  59.     recv(s,buf,BUFSIZE-1,0);
  60.     sprintf(title,"Got Connection");
  61.     settitle();
  62.  
  63.     sendgreet(s);
  64.  
  65.     if (senduser(s))
  66.     {
  67.         cleanup_sockets();
  68.         CloseLibrary( SockBase ) ;
  69.         return(1);
  70.     }
  71.  
  72.     count=havemail=sendstat(s);
  73.  
  74.     while (count && okay)
  75.     {
  76.         sprintf(title,"Retrieving %i",count);
  77.         settitle();
  78.  
  79.         okay=retrieve(s,count);
  80.  
  81.         if (delmail)
  82.         {
  83.             sprintf(title,"Deleting %i",count);
  84.             settitle();
  85.  
  86.             okay=delmessage(s,count);
  87.         }
  88.         --count;
  89.     }
  90.  
  91.     if(sendquit(s))
  92.     {
  93.         cleanup_sockets();
  94.         CloseLibrary( SockBase ) ;
  95.         return(1);
  96.     }
  97.  
  98.     if (Project0Wnd)
  99.     {
  100.         SetWindowTitles(Project0Wnd,Project0Wdt, (UBYTE *) ~0);
  101.     }
  102.  
  103.     if (havemail && notify) /* removed sendstat() after Nofify */
  104.     {
  105.         doreq("You have new mail.","Cool");
  106.     }
  107.  
  108. /* End actual code */
  109.  
  110.     s_close(s);
  111.     cleanup_sockets();
  112.     CloseLibrary( SockBase ) ;
  113.     return(0);
  114. }
  115.  
  116. int sendgreet(int s)
  117. {
  118.     sprintf(buf,"HELLO\r\n");
  119.     trans(s);
  120.  
  121.     return(0);
  122. }
  123.  
  124. int senduser(int s)
  125. {
  126.     int t;
  127.  
  128.     sprintf(title,"Sending Username");
  129.     settitle();
  130.  
  131.     sprintf(buf,"USER %s\r\n",username);
  132.  
  133.     if ( !trans(s) ) return(1);
  134.  
  135.     t=sscanf(buf,"%s",temp);
  136.     if ( valcheck(t,temp) ) return(1);
  137.  
  138. /* Password */
  139.  
  140.     sprintf(title,"Sending Password");
  141.     settitle();
  142.  
  143.     sprintf(buf,"PASS %s\r\n",password);
  144.  
  145.     if ( !trans(s) ) return(1);
  146.  
  147.     t=sscanf(buf,"%s",temp);
  148.  
  149.     if ( valcheck(t,temp) ) return(1);
  150.  
  151.     return(0);
  152. }
  153.  
  154. int sendquit(int s)
  155. {
  156.     int t;
  157.  
  158.     sprintf(title,"Sending QUIT");
  159.     settitle();
  160.  
  161.     sprintf(buf,"QUIT\r\n");
  162.  
  163.     if ( !trans(s) ) return(1);
  164.  
  165.     t=sscanf(buf,"%s",temp);
  166.  
  167.     if ( valcheck(t,temp) ) return(1);
  168.  
  169.     sprintf(title,"Quit Acknowledged");
  170.     settitle();
  171.  
  172.     return(0);
  173. }
  174.  
  175. int sendstat( int s )
  176. {
  177.     int t;
  178.     int count=0;
  179.  
  180.     sprintf(buf,"STAT\r\n");
  181.  
  182.     if ( !trans(s) ) return(0);
  183.  
  184.     t=sscanf(buf,"%s %i",temp,&count);
  185.  
  186.     if ( valcheck(t,temp) ) return(0);
  187.  
  188.     return(count);
  189. }
  190.  
  191. int retrieve(int s,int count)
  192. {
  193.     int foo;
  194.     int c;
  195.     int goon=1;
  196.  
  197.     char *havefrom;
  198.     BOOL begin=TRUE;
  199.     BPTR ofp;
  200.  
  201.     if (havefrom=AllocVec(512,MEMF_CLEAR))
  202.     {
  203.         if ( (count == havemail) && (!appfile) )
  204.         {
  205.             ofp = Open(maildir, MODE_NEWFILE );
  206.         }
  207.         else
  208.         {
  209.             ofp = Open(maildir, MODE_READWRITE);
  210.         }
  211.  
  212.         if ( ofp == 0)
  213.         {
  214.             doreq("Open() failed\n",bum);
  215.             FreeVec(havefrom);
  216.             return(0);
  217.         }
  218.  
  219.         Seek(ofp,0 ,OFFSET_END);
  220.  
  221.         sprintf(buf,"RETR %i\r\n",count);
  222.  
  223.         if ( !trans(s) )
  224.         {
  225.             Close(ofp);
  226.             FreeVec(havefrom);
  227.             return(0);
  228.         }
  229.  
  230.         if ( valcheck(1,buf) )
  231.         {
  232.             Close(ofp);
  233.             FreeVec(havefrom);
  234.             return(0);
  235.         }
  236.  
  237.         if (strstr(buf,"\r\n"))
  238.         {
  239.             FPuts(ofp,strstr(buf,"\r\n")+2);
  240.         }
  241.  
  242.         while ( goon )
  243.         {
  244.             foo=recv(s,buf,BUFSIZE-1,0);
  245.             buf[foo]='\0';
  246.  
  247.             goon=lastblock(buf);
  248.  
  249.             if (foo < 1)
  250.             {
  251.                 goon=0;
  252.             }
  253.  
  254.             if (begin) /* Magic to add From header */
  255.             {
  256.                 if (buf[0] != 'F')
  257.                 {
  258.                     struct DateTime dt;
  259.                     char day[20]="";
  260.                     char date[20]="";
  261.                     char time[20]="";
  262.                     char *newtemp;
  263.  
  264.                     if (newtemp=AllocVec(2*BUFSIZE,MEMF_CLEAR))
  265.                     {
  266.                         DateStamp(&dt.dat_Stamp);
  267.  
  268.                         dt.dat_Format = FORMAT_USA;
  269.                         dt.dat_Flags = 0;
  270.                         dt.dat_StrDay = day;
  271.                         dt.dat_StrDate = date;
  272.                         dt.dat_StrTime = time;
  273.  
  274.                         DateToStr(&dt);
  275.  
  276.                         sprintf(newtemp,"From %s@%s %s %s %s\n", username,pophost, day, date, time);
  277.                         FPuts(ofp,newtemp);
  278.                         FreeVec(newtemp);
  279.                     }
  280.                 }
  281.             }
  282.             begin=FALSE;
  283.  
  284.             strip();
  285.  
  286.             c=FPuts(ofp,buf);
  287.  
  288.             if (c != 0)
  289.             {
  290.                 doreq("FPuts() failed!",bum);
  291.                 goon=0;
  292.             }
  293.         }
  294.  
  295.         FPuts(ofp,"\n");
  296.         Close(ofp);
  297.  
  298.         FreeVec(havefrom);
  299.         return(1);
  300.     }
  301.     else
  302.     {
  303.         return(0);
  304.     }
  305.  
  306. }
  307.  
  308. int delmessage( int s, int count )
  309. {
  310.     sprintf(buf,"DELE %i\r\n",count);
  311.     trans(s);
  312.  
  313.     if ( valcheck(1,buf) ) return(0);
  314.  
  315.     return(1);
  316. }
  317.  
  318. int valcheck(int t, char *localtemp)
  319. {
  320.     if (t == 0)
  321.     {
  322.         doreq("scanf() failed\n",bum);
  323.         return(1);
  324.     }
  325.  
  326.     if ( !strstr(localtemp,"+OK") )
  327.     {
  328.         doreq("Didn't get +OK",bum);
  329.  
  330.         if (localtemp[0] != '\0')
  331.         {
  332.             doreq(localtemp,bum);
  333.         }
  334.         return(1);
  335.     }
  336.     return(0);
  337. }
  338.  
  339. int trans( int s )
  340. {
  341.     ULONG foo;
  342.  
  343.     send(s,buf,strlen(buf),0);
  344.  
  345.     foo=recv(s,buf,BUFSIZE-1,0);
  346.     buf[foo]='\0';
  347.  
  348.     if ( !foo )
  349.     {
  350.         return(0);
  351.     }
  352.  
  353.     return (1);
  354. }
  355.  
  356. void settitle( void )
  357. {
  358.     if (winop)
  359.     {
  360.         SetWindowTitles(Project0Wnd,title, (UBYTE *) ~0);
  361.     }
  362. }
  363.  
  364. void strip( void )
  365. {
  366.     char out[BUFSIZE]="";
  367.     ULONG x1=0;
  368.     ULONG x2=0;
  369.     ULONG len;
  370.  
  371.     len=strlen(buf);
  372.  
  373.     while (buf[x1] != '\0' )
  374.     {
  375.         while (buf[x1] == '\r')
  376.         {
  377.             ++x1;
  378.         }
  379.         out[x2]=buf[x1];
  380.         ++x2;
  381.         if (x1 < len)
  382.         {
  383.             ++x1;
  384.         }
  385.     }
  386.     out[x2]='\0';
  387.  
  388.     strcpy(buf,out);
  389. }
  390.  
  391. int lastblock (char *segment)
  392. {
  393.     char *found;
  394.     int len = strlen(segment);
  395.  
  396.     if ( (found=strstr(segment,"\r\n.\r\n")))
  397.     {
  398.         buf[(found-segment)-1]='\n';
  399.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  400.         return(0);
  401.     }
  402.  
  403.     if ( (len==2) && (found=strstr(segment,"\r\n")) )
  404.     {
  405.         return(0);
  406.     }
  407.  
  408.     if ( len==1 ) /* Kludge of the century */
  409.     {
  410.         return(0);
  411.     }
  412.  
  413.     if ( (len <= 4) && (found=strstr(segment,".\r\n")) )
  414.     {
  415.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  416.         return(0);
  417.     }
  418.  
  419.     return(1);
  420. }